<?php
namespace app\admin\controller;

use app\BaseController;
use app\model\Admin;
use app\common\token\TokenService;
use app\common\file\FileService;

class Login extends BaseController
{
    /**
     * 管理员登录
     *
     * @return void
     */
    public function login()
    {
        $admin_login_data = [
            'username' => input('username'),
            'password' => input('password')
        ];

        if (empty($admin_login_data['username']) || empty($admin_login_data['password'])) {
            error('用户名密码不能为空');
        }

        $res = Admin::login($admin_login_data);

        $jwt_data = TokenService::getToken($res['id']);
        success('登录成功', ['token' => $jwt_data]);
    }

     /**
     * 上传文件
     *
     * @return void
     */
    public function upload()
    {
        $file = \request()->file('file');
        if (!$file) {
            error('文件不能为空');
        }
        $res = FileService::uploadFile($file);
        success('成功', $res);
    }
}